package com.magicalcoder.pigeon.zookeeper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by hzhedongyu on 2017/6/9.
 * 不做缓存 每次都重新取一次
 */
public class ZookeeperProperties {

    private static Logger logger = LoggerFactory.getLogger(ZookeeperProperties.class);

    private static final String fileName = "zookeeper.properties";


    public static String getServer(){
        String server = getString("server");
        if(server==null || "".equals(server)){
            throw new RuntimeException("加载配置文件"+fileName+"属性server 失败 参考配置 server=localhost:2181 或 server=localhost:2181,localhost:2182");
        }else {
            return server;
        }
    }

    private static String getString(String key){
        InputStream inputStream = ZookeeperProperties.class.getClassLoader().getResourceAsStream(fileName);
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
            String value = properties.getProperty(key);
            if(value!=null){
                return value;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        logger.error("加载配置文件"+fileName+"属性"+key+" 失败,请检查classpath下是否存在"+fileName);
        throw new RuntimeException("加载配置文件"+fileName+"属性"+key+" 失败");
    }

}
